import router from './routers'
import store from './store'
import { clearHttpRequestingList } from './service/clearHttp'

const whiteList = ['/login'] // 白名单
router.beforeEach(async (to, from, next) => {
  console.log(to)
  let appId= localStorage.getItem('systemAppId')
  clearHttpRequestingList()
  const hasToken = 'token'
  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      console.log(store.getters.routes)
      const hasRoles = store.getters.routes && store.getters.routes.length > 0
      if (hasRoles) {
        console.log('有路由',to)
        next()
     
      } else {
        
        // let res = await store.dispatch('user/fetchUserInfo')
        const roles = [
          {
              path: '/organizationManage',
              meta: { title: '组织管理',icon:'side_zuzhiguanli' },
              component: 'Layout',
              // redirect: '/organizationManage/manage',
              name:'organization',
              children: [{
                  path: 'manage',
                  meta: { title: '组织管理', icon:'side_zuzhiguanli'},
                  name: 'organizationManage',
                  hidden: false,
                  component: 'organization/OrganizationManage'
              }
          ]
          }]
        // const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
        // router.addRoutes(accessRoutes)
        await store.dispatch('permission/generateRoutes', roles)
        
        
       
        next({ ...to, replace: true })
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
    }
  }
})
